'use strict';

const BaseController = require('./base');

class OrderController extends BaseController {
  constructor(ctx) {
    super(ctx, 'Order');
  }

  // 创建订单
  async create() {
    const { ctx } = this;
    const data = ctx.request.body;
    // 获取当前登录会员ID
    const memberId = ctx.state.member.id;
    data.memberId = memberId;
    
    const order = await ctx.service.order.createOrder(data);
    ctx.success(order);
  }

  // 获取我的订单列表
  async getMyOrders() {
    const { ctx } = this;
    const { status } = ctx.query;
    const memberId = ctx.state.member.id;
    
    const list = await ctx.service.order.getMemberOrders(memberId, status);
    ctx.success(list);
  }

  // 取消订单
  async cancel() {
    const { ctx } = this;
    const id = ctx.params.id;
    const memberId = ctx.state.member.id;
    
    const order = await ctx.service.order.cancelOrder(id, memberId);
    ctx.success(order);
  }

  // 支付订单
  async pay() {
    const { ctx } = this;
    const id = ctx.params.id;
    const { paymentMethod } = ctx.request.body;
    const memberId = ctx.state.member.id;
    
    const order = await ctx.service.order.payOrder(id, memberId, paymentMethod);
    ctx.success(order);
  }
}

module.exports = OrderController;
